package com.itboye.jigongbao.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.itboye.jigongbao.bean.CacheInfo;
import com.itboye.jigongbao.utils.Const;

import static com.itboye.jigongbao.utils.ByTimeUtils.timeFormat;

/**
 * Created by Administrator on 2017/12/20.
 */

public class DBManager {
    public static DBManager instance;

    private DBHelper mDbHelper;

    SQLiteDatabase mDataBase;

    public static DBManager getInstance() {
        return instance;
    }

    public void init(Context mContext) {
        instance = this;
        mDbHelper = new DBHelper(mContext);
    }

    public long insertCache(String uid, String typeKey, String lastTime, String data, String apiVer, boolean isPaging, String pageIndex) {

//        String sql = "INSERT INTO " + DBParams.TABLE_NAME + "(" + DBParams.COLUMN_ID + "," + DBParams.COLUMN_UID + "," + DBParams.COLUMN_TYPE_KEY + "," + DBParams.COLUMN_LAST_TIME + "," + DBParams.COLUMN_DATA + "," + DBParams.COLUMN_API_VER + ") VALUES(" + 1 + "," + uid + "," + typeKey + "," + lastTime + "," + data + "," + apiVer + ")";
//        Log.v(Const.Companion.getTAG_DEBUG(), sql);
//        mDataBase.execSQL(sql);
        if (queryCache(uid, typeKey, apiVer) != null) {
            //如果查询到了以前缓存的，则先删除
            if (isPaging) {
                if (queryCacheByPageIndex(uid, typeKey, apiVer, pageIndex) != null) {
                    //查询到以前有当前页码数据则删除
                    long count = deleteCache(uid, typeKey, apiVer, pageIndex);
                }
            } else {
                long count = deleteCache(uid, typeKey, apiVer);
                Log.v(Const.Companion.getTAG_DEBUG(), "delete result-----" + count);
            }
        } else {
            Log.v(Const.Companion.getTAG_DEBUG(), "delete result----- no cache");
        }
        mDataBase = mDbHelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(DBParams.COLUMN_UID, uid);
        contentValues.put(DBParams.COLUMN_TYPE_KEY, typeKey);
        contentValues.put(DBParams.COLUMN_LAST_TIME, lastTime);
        contentValues.put(DBParams.COLUMN_DATA, data);
        contentValues.put(DBParams.COLUMN_API_VER, apiVer);
        contentValues.put(DBParams.COLUMN_INSERT_TIME, timeFormat("yyyy_MM_dd_HH_mm_ss", System.currentTimeMillis()));
        if (isPaging) {
            contentValues.put(DBParams.COLUMN_PAGE_INDEX, (int) Double.parseDouble(pageIndex) + "");
        }
        long re = 0;
        try {
            re = mDataBase.insert(DBParams.TABLE_NAME, null, contentValues);
            Log.v(Const.Companion.getTAG_DEBUG(), "insertCache re=" + re);
        } catch (Exception e) {
            e.fillInStackTrace();
        }
//        mDataBase.close();
        return re;
    }

    public void updateCacheByUid(String uid, String typeKey, String apiVer) {

    }

    public long deleteCache(String uid, String typeKey, String apiVer) {
        mDataBase = mDbHelper.getReadableDatabase();
        String where = (uid == null ? DBParams.COLUMN_TYPE_KEY + "=? AND " + DBParams.COLUMN_API_VER + " =?" : DBParams.COLUMN_UID + "=? AND " + DBParams.COLUMN_TYPE_KEY + "=? AND " + DBParams.COLUMN_API_VER + " =?");
        String[] whereArgs = uid == null ? new String[]{typeKey, apiVer} : new String[]{uid, typeKey, apiVer};
        Log.v(Const.Companion.getTAG_DEBUG(), where + "-----" + whereArgs.toString());
        long re = mDataBase.delete(DBParams.TABLE_NAME, where, whereArgs);
//        mDataBase.close();
        return re;
    }

    public long deleteCache(String uid, String typeKey, String apiVer, String pageIndex) {
        mDataBase = mDbHelper.getReadableDatabase();
        String where = (uid == null ? DBParams.COLUMN_TYPE_KEY + "=? AND " + DBParams.COLUMN_API_VER + " =?" : DBParams.COLUMN_UID + "=? AND " + DBParams.COLUMN_TYPE_KEY + "=? AND " + DBParams.COLUMN_API_VER + " =? AND " + DBParams.COLUMN_PAGE_INDEX + " =?");
        String[] whereArgs = uid == null ? new String[]{typeKey, apiVer, pageIndex} : new String[]{uid, typeKey, apiVer, pageIndex};
        Log.v(Const.Companion.getTAG_DEBUG(), where + "-----" + whereArgs.toString());
        long re = mDataBase.delete(DBParams.TABLE_NAME, where, whereArgs);
//        mDataBase.close();
        return re;
    }

    public CacheInfo queryCache(String uid, String typeKey, String apiVer) {
        mDataBase = mDbHelper.getReadableDatabase();
        CacheInfo cacheInfo = null;
        Cursor cursor =
                uid == null
                        ? mDataBase.query(DBParams.TABLE_NAME, null, DBParams.COLUMN_TYPE_KEY + "=? AND " + DBParams.COLUMN_API_VER + "=?", new String[]{typeKey, apiVer}, null, null, null) :
                        mDataBase.query(DBParams.TABLE_NAME, null, DBParams.COLUMN_UID + "=? AND " + DBParams.COLUMN_TYPE_KEY + "=? AND " + DBParams.COLUMN_API_VER + "=?", new String[]{uid, typeKey, apiVer}, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                String uuid = cursor.getString(1);
                String data = cursor.getString(4);
                cacheInfo = new CacheInfo(uuid, typeKey, "", data, apiVer);
                break;
            }
            cursor.close();
        }
//        mDataBase.close();
        return cacheInfo;
    }

    public CacheInfo queryCacheByPageIndex(String uid, String typeKey, String apiVer, String pageIndex) {
        mDataBase = mDbHelper.getReadableDatabase();
        CacheInfo cacheInfo = null;
        Cursor cursor =
                uid == null
                        ? mDataBase.query(DBParams.TABLE_NAME, null, DBParams.COLUMN_TYPE_KEY + "=? AND " + DBParams.COLUMN_API_VER + "=? AND " + DBParams.COLUMN_PAGE_INDEX + "=?", new String[]{typeKey, apiVer, pageIndex}, null, null, null) :
                        mDataBase.query(DBParams.TABLE_NAME, null, DBParams.COLUMN_UID + "=? AND " + DBParams.COLUMN_TYPE_KEY + "=? AND " + DBParams.COLUMN_API_VER + "=? AND " + DBParams.COLUMN_PAGE_INDEX + "=?", new String[]{uid, typeKey, apiVer, pageIndex}, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                String uuid = cursor.getString(1);
                String data = cursor.getString(4);
                cacheInfo = new CacheInfo(uuid, typeKey, "", data, apiVer);
                break;
            }
            cursor.close();
        }
        mDataBase.close();
        return cacheInfo;
    }
}
